package cn.lichenfei.fxui.controls.skins;

import cn.lichenfei.fxui.controls.CFAlert;
import javafx.scene.control.Label;
import javafx.scene.control.SkinBase;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsOutlined;
import org.kordamp.ikonli.javafx.FontIcon;

public class CFAlertSkin extends SkinBase<CFAlert> {

    private final FontIcon icon = new FontIcon();
    private final Label textLabel;
    private final HBox container;
    private final FontIcon closeIcon = FontIcon.of(AntDesignIconsOutlined.CLOSE);

    public CFAlertSkin(CFAlert control) {
        super(control);
        textLabel = new Label();
        container = new HBox(textLabel, closeIcon);
        HBox.setHgrow(textLabel, Priority.ALWAYS);
        textLabel.setMaxSize(Double.MAX_VALUE, Region.USE_PREF_SIZE);
        textLabel.setGraphic(icon);
        icon.setIconCode(control.getLevel().getIkon());
        //
        textLabel.getStyleClass().add("text-label");
        closeIcon.getStyleClass().add("close-icon");
        container.getStyleClass().add("container");
        getChildren().add(container);
        initEvent(control);
    }

    private void initEvent(CFAlert control) {
        textLabel.textProperty().bind(control.textProperty());
        textLabel.textFillProperty().bind(icon.iconColorProperty());
        closeIcon.managedProperty().bind(control.closableProperty());
        closeIcon.visibleProperty().bind(control.closableProperty());
        control.levelProperty().addListener((observable, oldValue, newValue) -> icon.setIconCode(newValue.getIkon()));
        closeIcon.onMouseClickedProperty().set(control.getHideEvent());// 关闭事件
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void layoutChildren(final double x, final double y,
                                  final double w, final double h) {
        super.layoutChildren(x, y, w, h);
    }
}
